package com.yiduo.outpost.core.web;

import com.yiduo.outpost.core.web.argument.PageableArgumentResolver;
import com.yiduo.outpost.core.web.argument.QueryWrapperArgumentResolver;
import com.yiduo.outpost.core.web.exception.WebMvcErrorAttributes;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author yanwen
 */
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
public class WebMvcAutoConfiguration implements WebMvcConfigurer {

    private static final String RELAXED_CHARS = "<>[\\]^`{|}";

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
        return factory -> factory.addConnectorCustomizers( connector -> {
            connector.setProperty("relaxedPathChars", RELAXED_CHARS);
            connector.setProperty("relaxedQueryChars", RELAXED_CHARS);
        });
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new QueryWrapperArgumentResolver());
        resolvers.add(new PageableArgumentResolver());
    }

    @Bean
    public ErrorAttributes errorAttributes(MessageSource messageSource) {
        return new WebMvcErrorAttributes(messageSource);
    }

//    @Bean
//    public RestTemplate restTemplate() {
//        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
//        RestTemplate restTemplate = new RestTemplate(requestFactory);
//        //提供对输入输出的缓冲，可以让响应 body多次读取
//        restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));
//        restTemplate.setInterceptors(Lists.newArrayList(new RestTemplateLoggingRequestInterceptor()));
//        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
//        for (HttpMessageConverter<?> messageConverter : messageConverters) {
//            if (messageConverter instanceof StringHttpMessageConverter) {
//                ((StringHttpMessageConverter) messageConverter).setDefaultCharset(StandardCharsets.UTF_8);
//            }
//        }
//        restTemplate.setMessageConverters(messageConverters);
//        return restTemplate;
//    }

//    @Bean
//    RestTemplate restTemplate(RestTemplateBuilder builder) {
//        return builder.build();
//    }

}
